/*
 * Copyright (C), 2002-2018, 南京视客网络科技有限公司
 * FileName: DateUtils.java
 * Author:   zhixiang.meng
 * Date:     2018年7月9日 下午4:26:56
 * Description: //模块目的、功能描述
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.lanp.gencode.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Calendar;
import java.util.Date;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author zhixiang.meng
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class DateUtils {

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM = "yyyy-MM";
    private final static Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);

    /**
     * date 类型转化成字符串
     *
     * @param date
     * @param patter
     * @return
     */
    public static String dateToString(Date date, String patter) {
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        return sdf.format(date);
    }

    /**
     * 根据格式要求，获取当前时间
     *
     * @param patter
     * @return
     */
    public static Date nowByDate(String patter) {
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        Date date = new Date();
        try {
            return sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            LOGGER.error(e.getMessage());
        }
        return null;
    }

    /**
     * 传入日期格式，转化个需要的格式
     *
     * @param patter
     * @return
     */
    public static Date byDate(Date date, String patter) {
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        try {
            return sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            LOGGER.error(e.getMessage());
        }
        return null;
    }

    /**
     * 传入日期格式，转化个需要的格式
     *
     * @param patter
     * @return
     */
    public static Date byString(String date, String patter) {
        SimpleDateFormat sdf = new SimpleDateFormat(patter);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            LOGGER.error(e.getMessage());
        }
        return null;
    }

    /**
     * 获取时间差（年-月-日）方法
     *
     * @param
     * @return
     */
    public static String getTime(Date currentTime, Date firstTime) {
        long diff = currentTime.getTime() - firstTime.getTime();//这样得到的差值是微秒级别
        Calendar currentTimes = dataToCalendar(currentTime);//当前系统时间转Calendar类型
        Calendar firstTimes = dataToCalendar(firstTime);//查询的数据时间转Calendar类型
        int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR);//获取年
        int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH);
        int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH);
        if (day < 0) {
            month -= 1;
            //处理天数
            day = firstTimes.getActualMaximum(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH) + currentTimes.get(Calendar.DAY_OF_MONTH);
            //            currentTimes.add(Calendar.MONTH, -1);
            //            day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//获取日
        }
        if (month < 0) {
            month = (month + 12) % 12;//获取月
            year--;
        }
        long days = diff / (1000 * 60 * 60 * 24);
        String CountTime = year + "-" + month + "-" + day;
        return CountTime;
    }

    //Date类型转Calendar类型
    public static Calendar dataToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * 获取相差的天数
     *
     * @param
     * @return
     */
    public static long getDifferenceDay(Date currentTime, Date firstTime) {
        long diff = currentTime.getTime() - firstTime.getTime();//这样得到的差值是微秒级别
        long days = (long) Math.ceil(diff / (1000 * 60 * 60 * 24));
        return days;
    }

    /**
     * 获取相差的秒数
     *
     * @param
     * @return
     */
    public static long getDifferenceSecond(Date currentTime, Date firstTime) {
        long diff = currentTime.getTime() - firstTime.getTime();//这样得到的差值是微秒级别
        long second = (long) Math.ceil(diff / (1000));
        return second;
    }

    /**
     * 当前日期加上天数后的日期
     *
     * @param num 为增加的天数
     * @return
     */
    public static String plusDay(int num) {
        Date d = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currdate = format.format(d);
        System.out.println("现在的日期是：" + currdate);

        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
        d = ca.getTime();
        String enddate = format.format(d);
        System.out.println("增加天数以后的日期：" + enddate);
        return enddate;
    }

    /**
     * 获取当前 年月
     *
     * @return
     */
    public static String currentMonth() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }

    public static Date stringToDate(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date date = sdf.parse(dateString);
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取时间是周几
     *
     * @param date
     * @return
     */
    public static String getDateWeekday(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        return sdf.format(date);
    }

    /**
     * 获取相差的毫秒值
     *
     * @param currentTime
     * @param firstTime
     * @return
     */
    public static long getDifferenceMilliSecond(Date currentTime, Date firstTime) {
        //这样得到的差值是微秒级别
        long diff = currentTime.getTime() - firstTime.getTime();
        return diff;
    }

    public static Long addDays(int days) {
        Calendar calendar = dataToCalendar(new Date());
        calendar.add(Calendar.DATE, days);
        return calendar.getTime().getTime();
    }

    public static Date getNowDate() {
        return new Date();
    }

    public static String getNowDateStr() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return sdf.format(new Date());
    }

    /**
     * 开始时间加多少天之后
     *
     * @param addDay    天数
     * @param startTime 开始时间
     * @return
     */
    public static Date addDay2(int addDay, Date startTime) {
        Calendar calendar = dataToCalendar(startTime);
        calendar.add(Calendar.DATE, addDay);
        return calendar.getTime();
    }

    /**
     * 判断当前时间点是否在指定事件点范围内
     *
     * @param bTime 开始时间
     * @param eTime 结束时间
     * @return
     */
    public static boolean isTimeRange(String bTime, String eTime) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("HH");
        Date now = df.parse(df.format(new Date()));
        Date begin = df.parse(bTime);
        Date end = df.parse(eTime);
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(now);
        Calendar beginTime = Calendar.getInstance();
        beginTime.setTime(begin);
        Calendar endTime = Calendar.getInstance();
        endTime.setTime(end);
        if (nowTime.before(endTime) && nowTime.after(beginTime)) {
            return true;
        } else {
            return nowTime.compareTo(beginTime) == 0 || nowTime.compareTo(endTime) == 0;
        }
    }

    /**
     * 测试方法
     *
     * @param args
     */
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        LocalDate inDay = LocalDate.of(2018, 10, 20);
        LocalDate parse = LocalDate.parse("2018-10-20");
        Period between = Period.between(parse, today);
        int days = between.getDays();
        int months = between.getMonths();
        int years = between.getYears();
        System.out.println(years + "年" + months + "月" + days + "日");

    }

    /**
     * 时间戳转时间
     *
     * @param time
     * @return
     */
    public static Date longToDate(Long time) {
        Date nowDate = new Date();
        nowDate.setTime(time);
        return nowDate;
    }

}
